package org.example.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 工具类 DateUtil 时间
 */

public class DateUtil {
    //获取日历
    private static final Calendar calendar = Calendar.getInstance();


    /**
     * @param d java.util.Date
     * @return java.sql.Date 去掉了时分秒
     */
    public static java.sql.Date util2sql(Date d) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String format = sdf.format(d);
        try {
            Date parse = sdf.parse(format);
            return new java.sql.Date(parse.getTime());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 给 Calendar 对象赋值，赋此时此刻的时、分、秒、毫秒
     */
    public static void initToday() {
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.getTime();
    }


    /**
     * 给 Calendar 对象赋值，赋当月月初0点的时间
     *
     * @return 月初0点的时间
     */
    public static Date monthBegin() {
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * 这是一个包装的方法，实际调用下面的 public static Date monthEnd(Date start) 方法
     *
     * @return 返回当月的月末时间
     */
    public static Date monthEnd() {
        return monthEnd(monthBegin());
    }


    /**
     * @param start 月初时间
     * @return 月末0点的时间
     */
    public static Date monthEnd(Date start) {
        calendar.setTime(start);
        calendar.set(Calendar.DATE, 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


    /**
     * @return 本月总天数
     */
    public static int thisMonthTotalDay() {
        calendar.setTime(new Date());
        monthEnd();
        return calendar.get(Calendar.DATE);
    }


    /**
     * @return 本月剩余天数
     */
    public static int thisMonthLeftDay() {
        int totalDay = thisMonthTotalDay();
        initToday();
        int today = calendar.get(Calendar.DATE);
        return totalDay - today + 1;
    }


    /**
     * @return 本年年份数
     */
    public static int thisYear() {
        initToday();
        return calendar.get(Calendar.YEAR);
    }


    /**
     * @return 本年月份数
     */
    public static int thisMonth() {
        initToday();
        return calendar.get(Calendar.MONTH);
    }


    /**
     * @return 本年每月的第一天的0时0分0秒
     */
    public static List<Date> listMonthBeginOfThisYear() {
        List<Date> listMonth = new ArrayList<>();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);

        for (int i = 0; i < 12; i++) {
            calendar.set(Calendar.MONTH, i);
            listMonth.add(calendar.getTime());
        }
        return listMonth;
    }
}
